package com.xh.bussiness.course.controller;

import com.xh.bussiness.course.bean.request.CourseIndexRequest;
import com.xh.bussiness.course.service.ICourseIndexService;
import com.xh.bussiness.course.service.ICourseService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="课程课次接口")
@RestController
@RequestMapping("/courseIndex")
public class CourseIndexController {

    @Autowired
    private ICourseIndexService courseIndexService;
    @Autowired
    private ICourseService courseService;

    @PostMapping("/query")
    @ApiOperation(value="查询课次", notes = "查询课次")
    public AjaxResult query( @RequestBody CourseIndexRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",courseIndexService.query(bean));
    }

    @PostMapping("/add")
    @ApiOperation(value="添加课次", notes = "添加课次")
    public AjaxResult add(@RequestBody CourseIndexRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"添加成功",courseIndexService.add(bean));
    }

    @PostMapping("/batchSave")
    @ApiOperation(value="批量添加课次", notes = "批量添加课次")
    public AjaxResult batchSave(@RequestBody List<CourseIndexRequest> bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"批量添加成功",courseIndexService.batchSave(bean));
    }

    @PostMapping("/modify")
    @ApiOperation(value="修改课次", notes = "修改课次")
    public AjaxResult modify(@RequestBody CourseIndexRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"添加成功",courseIndexService.modify(bean));
    }

    @PostMapping("/batchSaveOrUpdate")
    @ApiOperation(value="批量修改课次", notes = "批量修改课次")
    public AjaxResult batchModify(@RequestBody List<CourseIndexRequest> bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"批量添加成功",courseIndexService.batchModify(bean));
    }

    @PostMapping("/batchUpdate")
    @ApiOperation(value="批量修改课次", notes = "批量修改课次")
    public AjaxResult batchUpdate(@RequestBody List<CourseIndexRequest> bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"批量添加成功",courseIndexService.batchUpdateCourseIndex(bean));
    }

    @PostMapping("/delete")
    @ApiOperation(value="删除课次", notes = "删除课次")
    public AjaxResult delete(String id){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "删除成功", courseIndexService.deleteCourseIndex(id));
    }

    @PostMapping("/batchDelete")
    @ApiOperation(value="批量删除课次", notes = "删除课次")
    public AjaxResult batchDelete(@RequestBody List<String> ids){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "删除成功", courseIndexService.batchDeleteCourseIndex(ids));
    }

    @PostMapping("/queryOne")
    @ApiOperation(value="获取课程课次信息", notes = "获取课程课次信息")
    public AjaxResult queryOne(String id){
        if(StringUtils.isEmpty(id)){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,courseIndexService.queryOne(id));
    }

    @RequestMapping("/modifyCourseIndexUpOrDown")
    @ApiOperation("课程课次上下移 1：上移，-1下移")
    public AjaxResult modifyCourseIndexUpOrDown(String courseIndexId, Integer moveType) {
        if (StringUtils.isEmpty(courseIndexId) || moveType == null) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(courseIndexService.modifyCourseIndexUpOrDown(courseIndexId, moveType));
    }

    @PostMapping("/deleteMaterial")
    @ApiOperation(value="获取课程课次信息", notes = "获取课程课次信息")
    public AjaxResult deleteMaterial(@RequestParam(required = false,defaultValue = "1") Integer type, String courseIndexId, String materialId){
        if(StringUtils.isEmpty(courseIndexId) || StringUtils.isEmpty(materialId)){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,courseIndexService.deleteMaterial(type, courseIndexId, materialId));
    }

    @PostMapping("/queryMaterials")
    @ApiOperation(value="查询课次的资料", notes = "查询课次的资料")
    public AjaxResult queryMaterials(String courseIndexId){
        if(StringUtils.isEmpty(courseIndexId)){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,courseIndexService.queryMaterials(courseIndexId));
    }

    @PostMapping("/addMaterials")
    @ApiOperation(value="查询课次的资料", notes = "查询课次的资料")
    public AjaxResult addMaterials(String courseIndexId, String materialId){
        if(StringUtils.isEmpty(courseIndexId)){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,courseIndexService.addMaterials(courseIndexId, materialId));
    }

    @PostMapping("/addPaper")
    @ApiOperation(value="添加试卷", notes = "添加试卷")
    public AjaxResult addPaper(String courseIndexId, String paperId){
        if(StringUtils.isEmpty(courseIndexId)){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,courseIndexService.addPaper(courseIndexId, paperId));
    }

    /**
     * 清空所有资料
     * @param courseIndexId
     * @return
     */
    @PostMapping("/clearAllMaterial")
    public AjaxResult clearAllMaterial(String courseIndexId){
        if(StringUtils.isEmpty(courseIndexId)){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,courseIndexService.clearAllMaterial(courseIndexId));
    }

    /**
     * 清空所有资料
     * @param courseId
     * @return
     */
    @PostMapping("/clearAll")
    public AjaxResult clearAll(String courseId){
        if(StringUtils.isEmpty(courseId)){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,courseIndexService.clearAll(courseId));
    }
}
